#include "SocketException.h"

#include <sstream>
#include <iostream>

SocketException::SocketException(int error): errorcode(error)
{
	if (!errorcode)
	{
			errorcode = WSAGetLastError();
	}


	LPVOID lpMsgBuf;

	FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, errorcode, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPSTR)&lpMsgBuf, 0, NULL);

	std::ostringstream bufstream;
	bufstream << "Socket error " << errorcode << ": " << (char*)lpMsgBuf;
	errorstring = bufstream.str();

	LocalFree(lpMsgBuf);
}

SocketException::~SocketException()
{
}

const char* SocketException::what() const throw()
{
	return errorstring.c_str();
}


